package ext.generic.project.reports;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.Enumeration;
import java.util.Vector;

import wt.fc.Persistable;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.org.OrganizationServicesHelper;
import wt.org.WTGroup;
import wt.projmgmt.resource.Deliverable;
import wt.util.WTException;

public class TianmaProjectHelper implements RemoteAccess, Serializable {

	private static final String PROJECT_RESOURCE = "ext.generic.project.reports.ProjectReportRB";
	private static String CLASSNAME = TianmaProjectHelper.class.getName();

	public TianmaProjectHelper() {
	}

	/**
	 * 使用远程方法,根据用户的搜索字符串,获取符合条件的用户列表
	 * 
	 * @param findStr
	 *            :搜索字符串
	 * @return Vector 用户列表
	 * @exception WTException
	 *                windchill基本的例外
	 **/
	public Vector getUserListVector(String findStr) throws WTException, RemoteException, IOException, Exception {
		if (!RemoteMethodServer.ServerFlag) {
			String s4 = "getUserListVector";
			Class aclass[] = { java.lang.String.class };
			Object aobj[] = { findStr };
			return (Vector) RemoteMethodServer.getDefault().invoke(s4, null, this, aclass, aobj);
		}
		Vector projectInfos = TianmaProjectUtil.getUserList(findStr);

		return projectInfos;
	}

	public Vector getAllGroup(String findStr) throws WTException, RemoteException, InvocationTargetException {
		if (!RemoteMethodServer.ServerFlag) {
			String s4 = "getAllDepGroup";
			Class aclass[] = { java.lang.String.class };
			Object aobj[] = { findStr };
			return (Vector) RemoteMethodServer.getDefault().invoke(s4, null, CLASSNAME, aclass, aobj);
		} else {
			Vector projectInfos = new Vector();
			projectInfos = getAllDepGroup(findStr);
			return projectInfos;
		}
	}

	/**
	 * 
	 * @param findStr
	 * @return
	 */
	public Vector getAllDepGroup(String findStr) {
		Vector group = new Vector();
		try {
			System.out.println("==============start find=============");
			Enumeration allGroup = OrganizationServicesHelper.manager.allGroups();
			System.out.println("==============find result:=============");
			Object thisObj = null;
			while (allGroup.hasMoreElements()) {
				System.out.println("==============first:=============");
				thisObj = (WTGroup) allGroup.nextElement();
				String groupName = "";
				groupName = ((WTGroup) thisObj).getName();
				System.out.println("groupName======>" + groupName);
				String groupDes = "";
				groupDes = ((WTGroup) thisObj).getDescription();
				System.out.println("groupDes======>" + groupDes);
				if (groupDes != null) {
					if (groupDes.equals("部门")) {
						group.add(groupName);
					}
				}
			}
			return group;
		} catch (WTException wte) {
			wte.printStackTrace();
			return null;
		}
	}

	/**
	 * 使用远程方法,获取各项目相关的交付项统计信息,并构建适合页面输出的表
	 * 
	 * @param
	 * @return NmDefaultHTMLTable 包含项目的表格
	 * @exception WTException
	 *                windchill基本的例外
	 **/
	public Vector getAllDeliverOfProject(String projOid) throws WTException, RemoteException, IOException, Exception {
		if (!RemoteMethodServer.ServerFlag) {
			String s4 = "getAllDeliverOfProject";
			Class aclass[] = { java.lang.String.class };
			Object aobj[] = { projOid };
			return (Vector) RemoteMethodServer.getDefault().invoke(s4, null, this, aclass, aobj);
		}
		Vector deliverInfos = TianmaProjectUtil.getDeliverByPjForCurrUser(projOid);

		return deliverInfos;
	}

	public Persistable getTargetOfDeliverable(Deliverable deliverable) throws WTException, RemoteException, IOException, Exception {
		if (!RemoteMethodServer.ServerFlag) {
			String s4 = "getTargetOfDeliverable";
			Class aclass[] = { wt.projmgmt.resource.Deliverable.class };
			Object aobj[] = { deliverable };
			return (Persistable) RemoteMethodServer.getDefault().invoke(s4, null, this, aclass, aobj);
		}
		Persistable subject = TianmaProjectUtil.getTargetOfDeliverable(deliverable);

		return subject;
	}
}
